package com.demo.test;

import com.demo.mapper.UserMapper;
import com.demo.pojo.User;
import com.demo.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

public class UserTest2 {
    @Test
    public void testAdd(){
        //创建SqlSession实例
        SqlSession session = MyBatisUtils.getSession();
        //获取Mapper
        UserMapper userMapper = session.getMapper(UserMapper.class);

        User user = new User();
        user.setUname("测试用户001");//设置姓名(user对象的uname属性对应SQL中的#{uname}参数)
        user.setUage(25); //设置姓名(user对象的uage属性对应SQL中的#{uage}参数)
        int rows = userMapper.add(user);
        session.commit(); //提交后才能写入数据库
        System.err.println("新增记录的id是："+user.getUid());
        System.err.println("查询影响行数："+rows);
        session.close();
    }

    @Test
    public void testUpdate(){
        //创建SqlSession实例
        SqlSession session = MyBatisUtils.getSession();
        //获取Mapper
        UserMapper userMapper = session.getMapper(UserMapper.class);

        User user = new User();
        user.setUname("测试用户007"); //设置新的姓名(对应SQL中的#{uname}参数)
        user.setUage(30); //设置新的年龄(对应SQL中的#{uage}参数)
        user.setUid(7);  //要更新记录的id(对应SQL中的#{uid}参数)
        int rows = userMapper.update(user);
        session.commit();//提交后才能写入数据库
        System.err.println("查询影响行数："+rows);
        session.close();
    }

    @Test
    public void testDelete() {
        //创建SqlSession实例
        SqlSession session = MyBatisUtils.getSession();
        //获取Mapper
        UserMapper userMapper = session.getMapper(UserMapper.class);
        //调用Mapper接口方法
        int rows = userMapper.delete(7);
        session.commit();//提交后才能写入数据库
        System.err.println("查询影响行数："+rows);
        session.close();
    }
}
